using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;

namespace Wax.ConsoleRunner
{
    class Program
    {
        /// <summary>
        /// Mains program entry point. Returns a -1 system exit code
        /// if any failures occured during the running of the tests.
        /// </summary>
        /// <param name="args">The args.</param>
        [STAThread]
        public static void Main(string[] args)
        {
            string scriptFileName = null;

            foreach (string arg in args)
            {
                if (arg.StartsWith("/file:", StringComparison.InvariantCultureIgnoreCase))
                {
                    scriptFileName = arg.Substring(6);
                }
            }

            if (string.IsNullOrEmpty(scriptFileName))
            {
                WriteSuccess(Resource.ArgumentValidationFileRequired);
                ShowUsage();
                return;
            }

            if (!File.Exists(scriptFileName))
            {
                scriptFileName = Path.Combine(System.Environment.CurrentDirectory, scriptFileName);    
            }

            if (!File.Exists(scriptFileName))
            {
                WriteError(string.Format(CultureInfo.InvariantCulture, Resource.ArgumentValidationFileNotFound, scriptFileName));
                ShowUsage();
                return;
            }

            ConsoleRunner consoleRunner = new ConsoleRunner(scriptFileName);

            bool success = true;
            
            try
            {
                consoleRunner.Run();
            }
            catch (Exception exception)
            {
                success = false;
                WriteError(exception.ToString());
            }
            finally
            {
                if (!success || consoleRunner.ErrorsOccured)
                {
                    System.Environment.Exit(-1);
                }
            }

        }

        /// <summary>
        /// Writes an error message in red.
        /// </summary>
        /// <param name="message">The message.</param>
        internal static void WriteError(string message)
        {
            WriteColorMessage(message, ConsoleColor.Red);
        }

        /// <summary>
        /// Writes a success message in green.
        /// </summary>
        /// <param name="message">The message.</param>
        internal static void WriteSuccess(string message)
        {
            WriteColorMessage(message, ConsoleColor.Green);
        }

        /// <summary>
        /// Writes a color message to the console.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="color">The color.</param>
        private static void WriteColorMessage(string message, ConsoleColor color)
        {
            ConsoleColor previousColor = Console.ForegroundColor;
            Console.ForegroundColor = color;
            Console.WriteLine(message);
            Console.ForegroundColor = previousColor;
        }

        /// <summary>
        /// Shows the usage.
        /// </summary>
        private static void ShowUsage()
        {
            Console.Write("Wax /file:path");
            Console.Write("\tFile: full path to test script");
        }        
    }
}
